package com.asen.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.asen.alibaba.service.CustomerFallBackHandler;
import com.asen.alibaba.service.OrderService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @ClassName OrdelServicew
 * @Deacription TODO
 * @Author chenxin
 * @Date 2021/3/24 14:58
 **/
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    OrderService orderService;
    int a = 0;
    @GetMapping("create")
    @SentinelResource(value = "createOrder",
            blockHandlerClass = CustomerFallBackHandler.class,
            blockHandler = "handleException",
            fallbackClass = CustomerFallBackHandler.class,
            fallback = "fallbackException"
    )
    @SneakyThrows
    public String createOrder() {
        if (a < 4) {
            a++;
        }else {
            a = 3;
            throw new IOException();
        }
        return orderService.createOrder();
    }
}
